<?php

namespace app\common\service\ai;

use think\facade\Db;
use app\common\service\CommonService;

/**
 * 会员
 */
class MemberService extends CommonService {

    /**
     * 表名
     * @static string
     */
    protected static $table = 'member';
    
    /**
     * 列表
     * @param array $data 数据
     * @return array
     */
    public static function getListByAdmin($data) {
        $list = self::query(self::$table)
            ->_like('a.nickname#keyword,a.username#keyword,a.phone#keyword,a.member_id#keyword')
            ->_dateBetween('a.create_time#create_time')
            ->order('a.member_id desc')
            ->_page(true);
        foreach ($list['list'] as &$value) {
            $value['is_promoter'] = PromoterService::isPromoter($value['member_id']);
        }
        return $list;
    }
    
    /**
     * 详情
     * @param array $data 数据
     * @return array
     */
    public static function getDetailByAdmin($data) {
        return self::query(self::$table)->where(['id' => self::ID($data)])->_find();
    }
    
    /**
     * 开户
     * @param array $data [member_id会员ID]
     * @return array
     */
    public static function openAccount($data) {
        $member = self::query(self::$table)->where(['member_id' => $data['member_id']])->_find();
        !$member && self::error();
        Db::startTrans();
        // 订单
        $order_no = OrderService::createOrerNo();
//        $sale_price = SystemConfigService::getDetail('OPEN_ACCOUNT_FEE')['value'] ?: 0;
        $sale_price = $data['sale_price'] ?? 0;
        $point_rate = SystemConfigService::getDetail('POINT_EXCHANGE')['value'] ?: 10;
        $point = $sale_price * $point_rate;
        $order_data = [
            'order_no' => $order_no,
            'member_id' => $data['member_id'],
            'type' => 0,
            'scene_type' => 1,
            'point' => $point,
            'sale_price' => $sale_price,
            'point_rate' => $point_rate,
            'before_point' => $member['remain_point'],
            'after_point' => $member['remain_point'] + $point,
            'remark' => OrderService::getRemark(0, 0, 1),
        ];
        $order_id = self::model()->_insert('order', $order_data);
        if (!$order_id) {
            Db::rollback();
            self::error();
        }
        // 用户充点
//        $result = self::model()->_update('member', ['point' => Db::raw("point+{$point}"), 'remain_point' => Db::raw("remain_point+{$point}")], ['member_id' => $data['member_id']]);
//        if (!$result) {
//            Db::rollback();
//            self::error();
//        }
        Db::commit();
        self::success(['order_no' => $order_no]);
    }
    
    /**
     * 新增
     * @param array $data 数据
     * @return mixed
     */
    public static function add($data) {
        // 密码
        if (isset($data['password'])) {
            $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
        }
        $result = self::model()->_insert(self::$table, $data, false);
        $result ? self::success() : self::error();
    }
    
    /**
     * 编辑
     * @param array $data 数据
     * @return mixed
     */
    public static function edit($data) {
        // 密码
        if (isset($data['password'])) {
            $data['pwd_time'] = datetime();
            $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
        }
        $result = self::model()->_update(self::$table, $data, ['member_id' => self::ID($data)], false);
        $result ? self::success() : self::error();
    }
    
    /**
     * 充点
     * @param array $data 数据
     * @return mixed
     */
    public static function recharge($data) {
        Db::startTrans();
        $result = OrderService::handle($data, 1);
        if ($result['code'] != 200) {
            Db::rollback();
            self::error();
        }
        Db::commit();
        self::success();
    }
    
    /**
     * 扣点
     * @param array $data 数据
     * @return mixed
     */
    public static function reduce($data) {
        Db::startTrans();
        $result = OrderService::handle($data, 2);
        if ($result['code'] != 200) {
            Db::rollback();
            self::error();
        }
        Db::commit();
        self::success();
    }
    
    /**
     * 退点
     * @param array $order_no 
     * @return mixed
     */
    public static function refund($order_no) {
        // 订单
        $order = self::query('order')->where(['order_no' => $order_no])->_find();
        if (!$order) {
            return self::error_out('订单不存在');
        }
        $order_result = self::model()->_update('order', ['status' => 0], ['order_no' => $order_no]);
        if (!$order_result) {
            return self::error_out('订单更新失败');
        }
        // 用户
        $result = self::model()->_update(self::$table, ['remain_point' => Db::raw("remain_point+{$order['point']}")], ['member_id' => $order['member_id']]);
        if (!$result) {
            return self::error_out('用户退回点数失败');
        }
        return self::success_out();
    }

}
